package com.chy.blog_server.modular.map.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chy.blog_server.core.util.GetUserIpAddressUtil;
import com.chy.blog_server.core.util.StringUtil;
import com.chy.blog_server.modular.map.entity.MapDistrict;
import com.chy.blog_server.modular.map.entity.MapLog;
import com.chy.blog_server.modular.map.mapper.MapLogMapper;
import com.chy.blog_server.modular.map.service.IMapLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 描述：地图抓取数据日志 service业务层
 * 作者：chenhuiyan
 * 时间：2025/03/27 12:03
 */
@Service
public class MapLogServiceImpl extends ServiceImpl<MapLogMapper, MapLog> implements IMapLogService {

    private static final Logger logger = LoggerFactory.getLogger(MapLogServiceImpl.class);

    @Autowired
    private MapDistrictServiceImpl mapDistrictService;

    /**
     * 查询列表-分页
     * @param params
     * @return
     */
    public List<Map<String, Object>> listPage(Map<String, Object> params){
        chineseConvertMap(params); //省份/城市代码转中文
        return baseMapper.listPage(params);
    }

    /**
     * 查询列表-不分页
     * @param params
     * @return
     */
    public List<Map<String, Object>> listAll(Map<String, Object> params){
        chineseConvertMap(params); //省份/城市代码转中文
        return baseMapper.listAll(params);
    }

    /**
     * 查询列表-实体-不分页
     * @param params
     * @return
     */
    public List<MapLog> listAllEntity(Map<String, Object> params){
        chineseConvertMap(params); //省份/城市代码转中文
        return baseMapper.listAllEntity(params);
    }

    /**
     * 查询总数-不分页使用
     * @param params
     * @return
     */
    public int getCountAll(Map<String, Object> params){
        chineseConvertMap(params); //省份/城市代码转中文
        return baseMapper.getCountAll(params);
    }

    /**
     * 查询总数-分页使用
     * @param params
     * @return
     */
    public int getCount(Map<String, Object> params){
        chineseConvertMap(params); //省份/城市代码转中文
        return baseMapper.getCount(params);
    }


    /**
     * 彻底删除
     * @param id
     * @return
     */
    public int thoroughlyDelete(String id){
        return baseMapper.thoroughlyDelete(id);
    }

    /**
     * 创建日志信息
     * @param logCode 日志编号
     * @param platform 平台(gaode高德,baidu百度)
     * @param logType 日志类型(1抓取,2导出)
     * @param province 省份
     * @param city 城市
     * @param keyworks 关键词
     * @param startIndex 数据开始下标
     * @param endIndex 数据结束下标
     * @param totalNum 数据总数
     * @param content 日志内容
     * @param operateIp 操作IP
     * @return
     */
    public MapLog createLogInfo(String logCode, String platform, String logType, String province, String city, String keyworks,
                                  int startIndex, int endIndex, int totalNum, String content, String operateIp){
        MapLog mapLog = new MapLog();
        mapLog.preInsert();
        mapLog.setLogCode(logCode);
        mapLog.setPlatform(platform);
        mapLog.setLogType(logType);
        mapLog.setProvince(province);
        mapLog.setCity(city);
        mapLog.setKeyworks(keyworks);
        mapLog.setStartIndex(startIndex);
        mapLog.setEndIndex(endIndex);
        mapLog.setTotalNum(totalNum);
        mapLog.setContent(content);
        mapLog.setOperateIp(operateIp);
        return mapLog;
    }

    /**
     * 省份/城市代码转中文
     * @param params
     * @return
     */
    public void chineseConvertMap(Map<String, Object> params){
        String provinceCode = params.get("province") + ""; // 省份
        String cityCode = params.get("city") + ""; // 城市
        //获取省份和城市中文名称
        if(StringUtil.notNull(provinceCode) && !"-1".equals(provinceCode)){
            MapDistrict provinceDistrict = mapDistrictService.getOne(new QueryWrapper<MapDistrict>().eq("level", "province").eq("adcode", provinceCode));
            if(ObjectUtil.isNotNull(provinceDistrict)){
                String province = provinceDistrict.getName(); // 省份中文名称
                params.put("province", province);
                if(provinceCode.equals(cityCode)){
                    params.put("city", province);
                }
            }
        }
        if(!provinceCode.equals(cityCode) && StringUtil.notNull(cityCode) && !"-1".equals(cityCode)){
            MapDistrict cityDistrict = mapDistrictService.getOne(new QueryWrapper<MapDistrict>().eq("level", "city").eq("adcode", cityCode));
            if(ObjectUtil.isNotNull(cityDistrict)){
                String city = cityDistrict.getName(); // 城市中文名称
                params.put("city", city);
            }
        }
    }

}
